<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('Entity/Members_Activecode_Entity.php');        
class Members_Activecode_Model extends Members_Activecode_Entity
{  
    public function __construct()
    {
        parent::__construct();
    }
  
    public function addNewActiveCode($mid, $type = 0)
    {
        $this->setMid($mid);
        $activeCode = imd5($mid);
        $this->setActivecode($activeCode);
        $this->setDate_created(time());
        $exp = time() + (3600 * 24 * 7); // ~1 tuan
        $this->setDate_exp($exp);
        $this->setTypes($type);
        return $this->save();
    }
    
    public function checkActiveCode($code)
    {
        $sql = "SELECT id, mid 
            FROM members_activecode 
            WHERE activecode = ? AND types = 0
                AND FROM_UNIXTIME(date_exp, '%Y-%m-%d') >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)";
        $query = $this->db->query($sql, array($code));
        return $query->row();
    }
    
    public function checkResetCode($code)
    {
       $sql = "SELECT id, mid 
            FROM members_activecode 
            WHERE activecode = ? AND types = 2
                AND FROM_UNIXTIME(date_exp, '%Y-%m-%d') >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)";
        $query = $this->db->query($sql, array($code));
        return $query->row(); 
    }
    
    public function Active($id)
    {
        $this->setId($id);
        $this->setTypes(1);
        $this->save();
    }

}